//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.trace;

import android.content.Context;
import android.os.Handler;
import android.preference.PreferenceManager;
import com.baidu.trace.a.c;
import com.baidu.trace.a.g;
import com.baidu.trace.a.j;
import com.baidu.trace.api.entity.LocRequest;
import com.baidu.trace.api.entity.OnEntityListener;
import com.baidu.trace.c.d;
import com.baidu.trace.c.e;
import com.baidu.trace.model.LocationMode;
import com.baidu.trace.model.TraceLocation;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class ar {
    private Context e = null;
    private WeakReference<LBSTraceService> f = null;
    private LocRequest g = null;
    private y h;
    private p i;
    private ak j;
    private g k = null;
    private ArrayList<j> l = null;
    private c m = null;
    private boolean n = true;
    private boolean o = false;
    private boolean p = true;
    private boolean q = true;
    private int r;
    private Handler s;
    private OnEntityListener t;
    protected static aj a = null;
    public static int b = 5000;
    public static int c = 1;
    public static int d = 600000;

    protected final void a() {
        TraceJniInterface.a().clearCustomData();
        TraceJniInterface.a().clearWifiData();
        TraceJniInterface.a().clearBluetoothData();
        TraceJniInterface.a().clearNearbyCells();
        g var1 = null;
        if(LocationMode.Battery_Saving.ordinal() != this.r) {
            int var10001 = b / 2;
            int var11 = 2 * b;
            int var5 = var10001;
            g var12 = new g();
            g var10000;
            if(this.j != null && e.e(this.e) && this.j.a(var5)) {
                this.j.a(var12, var11);
                if(var12.a) {
                    this.p = false;
                    this.q = false;
                    this.o = true;
                } else {
                    this.d();
                }

                var10000 = var12;
            } else {
                this.o = false;
                var12.a = false;
                this.d();
                var10000 = var12;
            }

            var1 = var10000;
            this.k = var1;
        }

        if(var1 != null && var1.a) {
            TraceJniInterface.a().setGPSData(var1.c, var1.d, var1.e, var1.f, var1.g, var1.h, var1.i);
        } else {
            TraceJniInterface.a().setGPSData(0, 0, 0, 0, 0, 0, 0);
        }

        ArrayList var2 = null;
        if(LocationMode.Device_Sensors.ordinal() != this.r) {
            ArrayList var22;
            if(!this.p) {
                var22 = null;
            } else {
                ArrayList var17 = new ArrayList();
                if(e.f(this.e) && this.h.a()) {
                    this.h.a(var17);
                }

                var22 = var17;
            }

            var2 = var22;
            this.l = var2;
        }

        if(var2 != null && var2.size() > 0) {
            for(int var3 = 0; var3 < var2.size(); ++var3) {
                j var4 = (j)var2.get(var3);
                TraceJniInterface.a().addWifiData(var4.a, var4.b, var4.c, var4.d);
            }
        }

        c var13 = null;
        if(LocationMode.Device_Sensors.ordinal() != this.r) {
            c var23;
            if(!this.q) {
                var23 = null;
            } else {
                c var18 = new c();
                if(e.g(this.e) && this.i.a()) {
                    this.i.a(var18);
                } else {
                    var18.a = false;
                }

                var23 = var18;
            }

            var13 = var23;
            this.m = var13;
        }

        if(var13 != null && var13.a) {
            TraceJniInterface.a().setCellData(var13.b, var13.c, var13.d, var13.e, var13.f, var13.g);

            for(int var14 = 0; var14 < var13.h.length && var14 < var13.i.length; ++var14) {
                TraceJniInterface.a().addNearbyCell(var13.h[var14], var13.i[var14]);
            }
        } else {
            TraceJniInterface.a().setCellData(0, 0, 0, 0, 0, 0);
        }

        long var16 = System.currentTimeMillis();
        if(var1 != null && var1.a && var1.b > 0L) {
            com.baidu.trace.a.a("L-GPS local time is: " + var1.b);
            if(Math.abs(System.currentTimeMillis() - var1.b) > (long)d) {
                var16 = var1.b;
            }
        }

        com.baidu.trace.a.a("L-GPS locTime is: " + var16);
        System.out.println("locTimeOffset : " + d);
        TraceJniInterface.a().addCustomData("_entity_name".getBytes(), z.c.getBytes());
        ArrayList var15 = this.a(var16);
        if(var15 != null && var15.size() > 0) {
            for(int var6 = 0; var6 < var15.size(); ++var6) {
                com.baidu.trace.a.e var7 = (com.baidu.trace.a.e)var15.get(var6);
                TraceJniInterface.a().addCustomData(var7.a, var7.b);
            }
        }

        byte[] var19 = TraceJniInterface.a().buildLocationData((int)(var16 / 1000L));
        if(var19 != null) {
            if(b <= 5000 && this.n && var1 != null && var1.a) {
                this.n = false;
            } else {
                ar.a var20 = new ar.a(z.c, var19, var16 / 1000L);
                if(bd.c != null) {
                    bd.c.offer(var20);
                }

                if(a != null && !a.a()) {
                    long var21 = var20.c;
                    if(var1 != null && var1.a) {
                        if(com.baidu.trace.c.a.a != null) {
                            com.baidu.trace.c.a.a.execute(new at(this, var1, var21));
                        }
                    } else {
                        if(this.g == null) {
                            this.g = new LocRequest(z.b);
                        }

                        aa.a(this.e, this.g, this.t);
                    }
                }

                if(var2 != null) {
                    var2.clear();
                }

                if(var15 != null) {
                    var15.clear();
                }

            }
        }
    }

    public ar(WeakReference<LBSTraceService> var1, Handler var2) {
        this.r = LocationMode.High_Accuracy.ordinal();
        this.s = null;
        this.t = null;
        this.f = var1;
        if(this.f != null && this.f.get() != null) {
            this.e = ((LBSTraceService)this.f.get()).getServiceContext();
            this.r = ((LBSTraceService)this.f.get()).getLocationMode();
        }

        this.h = new y(this.e);
        this.i = new p(this.e);
        this.j = new ak(this.e);
        this.s = var2;
        if(PreferenceManager.getDefaultSharedPreferences(this.e).getBoolean("same_process", false)) {
            com.baidu.trace.c.a.a(this.e, LBSTraceClient.class);
        } else {
            com.baidu.trace.c.a.a(this.e, LBSTraceService.class);
        }

        this.t = new as(this);
    }

    public ar(Context var1, Handler var2) {
        this.r = LocationMode.High_Accuracy.ordinal();
        this.s = null;
        this.t = null;
        this.e = var1;
        this.h = new y(this.e);
        this.i = new p(this.e);
        this.j = new ak(this.e);
        this.s = var2;
    }

    protected final void b() {
        if(this.j != null) {
            this.j.a();
            this.j = null;
        }

    }

    protected final void c() {
        this.b();
        if(com.baidu.trace.c.a.a != null && !PreferenceManager.getDefaultSharedPreferences(this.e).getBoolean("same_process", true) && "FinalizableDelegatedExecutorService".equals(com.baidu.trace.c.a.a.getClass().getSimpleName())) {
            com.baidu.trace.c.a.a();
        }

        if(this.h != null) {
            this.h.b();
            this.h = null;
        }

        if(this.i != null) {
            this.i.b();
            this.i = null;
        }

        if(this.l != null) {
            this.l.clear();
            this.l = null;
        }

        if(this.m != null) {
            this.m = null;
        }

        if(this.k != null) {
            this.k = null;
        }

        if(this.e != null) {
            this.e = null;
        }

        if(this.f != null) {
            this.f.clear();
            this.f = null;
        }

        if(this.s != null) {
            this.s = null;
        }

        if(a != null) {
            a.b();
            a = null;
        }

        TraceJniInterface.clearTrackData();
    }

    private ArrayList<com.baidu.trace.a.e> a(long var1) {
        if(this.f != null && null != this.f.get() && null != ((LBSTraceService)this.f.get()).getClientListener()) {
            ArrayList var3 = new ArrayList();
            Map var4 = null;

            try {
                if(this.f != null && this.f.get() != null) {
                    var4 = ((LBSTraceService)this.f.get()).getClientListener().gatherAttribute(var1);
                }
            } catch (Exception var5) {
                ;
            }

            if(var4 != null) {
                Iterator var7 = var4.entrySet().iterator();

                while(var7.hasNext()) {
                    Entry var2 = (Entry)var7.next();
                    com.baidu.trace.a.e var6 = new com.baidu.trace.a.e();
                    if(!"_entity_name".equals(var2.getKey())) {
                        var6.a = d.a((String)var2.getKey());
                        var6.b = d.a((String)var2.getValue());
                        var3.add(var6);
                    }
                }
            }

            return var3;
        } else {
            return null;
        }
    }

    protected final void d() {
        this.q = true;
        this.p = true;
        this.o = false;
    }

    protected final boolean a(Context var1, Handler var2, TraceLocation var3) {
        if(this.j != null && this.o) {
            TraceLocation var5 = var3;
            if(this.j == null) {
                return false;
            } else {
                g var7 = new g();
                this.j.a(var7, 10000);
                if(var7.a) {
                    var5.setLatitude((double)var7.i / 600000.0D);
                    var5.setLongitude((double)var7.h / 600000.0D);
                    var5.setRadius((double)var7.d);
                    var5.setAltitude(var7.g);
                    var5.setDirection((float)(var7.e << 1));
                    var5.setSpeed((float)var7.f / 100.0F);
                    var5.setTime(e.c());
                    e.a(var5);
                }

                return var7.a;
            }
        } else {
            Class var6 = ar.class;
            synchronized(ar.class) {
                var2.post(new au(this, var1));
                return false;
            }
        }
    }

    public final void a(int var1) {
        this.r = var1;
    }

    static class a {
        String a;
        byte[] b;
        long c;

        public a(String var1, byte[] var2, long var3) {
            this.a = var1;
            this.b = var2;
            this.c = var3;
        }
    }
}
